package com.zxx.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.zxx.ssm.dao.TeacherApplicationClass;
import com.zxx.ssm.model.biz.TeaAppBiz;
import com.zxx.ssm.model.form.TeaAppForm;
import com.zxx.ssm.model.result.BizResponse;
import com.zxx.ssm.service.TeaAppService;
import com.zxx.ssm.util.DateUtils;
import com.zxx.ssm.util.PaginationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("teaApp")
@Controller
public class TeaAppController {
    @Autowired
    private TeaAppService teaAppService;

    @RequestMapping("getAll")
    @ResponseBody
    public BizResponse getAll(@ModelAttribute TeaAppForm teaAppForm, @RequestParam(defaultValue = "1") int pageNumber, @RequestParam(defaultValue = "10") int pageSize,
                              @RequestParam(defaultValue = "id") String sort, @RequestParam(defaultValue = "desc") String order) {
        if("teaName".equals(sort))
            sort = "username";
        if("appTimeFormat".equals(sort))
            sort = "application_time";
        PaginationUtil.preset(pageNumber, pageSize, sort, order);
        teaAppForm.setTimeStart(DateUtils.convertString(teaAppForm.getTimeStart()));
        teaAppForm.setTimeEnd(DateUtils.convertString(teaAppForm.getTimeEnd()));
        List<TeaAppBiz> teaAppBizList = teaAppService.getAll(teaAppForm);
        PageInfo pageInfo = new PageInfo(teaAppBizList);
        BizResponse bizResponse = new BizResponse();
        bizResponse.put("teaAppList", pageInfo);
        return bizResponse;
    }

    @RequestMapping(value = "updateTeaApp", method = RequestMethod.POST)
    @ResponseBody
    public BizResponse updateTeaApp(@ModelAttribute TeacherApplicationClass teacherApplicationClass) {
        TeacherApplicationClass teaApp = teaAppService.selectByPrimaryKey(teacherApplicationClass.getId());
        teaApp.setResponse(teacherApplicationClass.getResponse());
        teaApp.setState(teacherApplicationClass.getState());
        teaAppService.updateByPrimaryKey(teaApp);
        return new BizResponse();
    }
}
